#!/usr/bin/env python
'''
Created on 31/ott/2012

@author: Arletti Luca 

'''

IMAGES = '/media/c6619bc0-34d2-4565-a3ed-ccf2e9043001/tinyImages/tiny_images.bin'
METADATA = '/media/c6619bc0-34d2-4565-a3ed-ccf2e9043001/tinyImages/tiny_metadata.bin'

IMGSIZE = 3072
METADATASIZE = 768

STEP = 10000

def subsample_dataset():
    print 'sampling dataset...'
    f_in = open(IMAGES, 'rb')
    f_out = open(IMAGES + '.sample', 'wb')
    count = 0
    while True:
        f_in.seek(count * IMGSIZE * STEP)
        buffer = f_in.read(IMGSIZE)
        if (len(buffer) == IMGSIZE):
            f_out.write(buffer)
        else:
            break
        count += 1
    return count

def subsample_metadata():
    print 'sampling metadata...'
    f_in = open(METADATA, 'rb')
    f_out = open(METADATA + '.sample', 'wb')
    count = 0
    while True:
        f_in.seek(count * METADATASIZE * STEP)
        buffer = f_in.read(METADATASIZE)
        if (len(buffer) == METADATASIZE):
            f_out.write(buffer)
        else:
            break
        count += 1   
    return count

if __name__ == '__main__':
    n = subsample_dataset()
    print 'sampled %d images' % (n,)
    n = subsample_metadata()
    print 'sampled %d metadata' % (n,)
